import { createFormOption } from '@/utils/options'

export const formOption = function () {
  const self = this
  return createFormOption({
    span: 24,
    submitBtn: false,
    emptyBtn: false,
    column: [
      {
        label: '角色名称',
        prop: 'roleName',
        hide: true,
        rules: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        search: true,
        disabled: true,
        overHidden: true
      },
      {
        label: '选择部门',
        prop: 'departId',
        multiple: false,
        checkStrictly: true,
        props: {
          label: 'name',
          value: 'id',
          children: 'childList'
        },
        type: 'tree',
        filter: false,
        hide: true,
        search: true,
        dicData: self.departList,
        control: (_, form) => {
          form.postId = ''
          self.getPost()
          self.$nextTick(() => {
            self.getUser()
          })
        }
      },
      {
        label: '选择职务',
        prop: 'postId',
        props: {
          label: 'name',
          value: 'id'
        },
        hide: false,
        search: true,
        type: 'select',
        dicData: self.postList,
        control: (_, form) => {
          if (self.roleCode !== 'super_admin') {
            form.userId = ''
          }
          form.userIds = []
          self.getUser()
        }
      },
      // {
      //   label: "选择人员",
      //   prop: "userId",
      //   props: {
      //     label: "realname",
      //     value: "id",
      //   },
      //   display: self.roleCode === "super_admin",
      //   hide: true,
      //   type: "select",
      //   filter: false,
      //   multiple: false,
      //   search: true,
      //   dicData: self.userList,
      //   control: (val, form) => {
      //     if (val) {
      //       form.userIds = [val];
      //     }
      //   },
      // },
      {
        label: '选择人员',
        prop: 'userIds',
        props: {
          label: 'realname',
          value: 'id'
        },
        // display: self.roleCode !== "super_admin",
        hide: true,
        type: 'select',
        filter: false,
        multiple: self.roleCode !== 'super_admin',
        search: true,
        dicData: self.userList,
        control: (val) => {
          console.log("🤡 ~ val:", val)
          // 这边判断问题 会导致第一次超管回显不了
          self.$nextTick(() => {
          // if (!val || val.length == 0) return
          if (self.roleCode == 'super_admin') {
            // self.selectedUsersIds = [val]
            console.log("🤡 ~ [val]:", [val])
            self.selectedUsers = [val].map((id) => self.allUsersMap[id]).filter((row) => row && row.id)
            console.log("🤡 ~  self.selectedUsers:", self.allUsers, self.allUsersMap, self.selectedUsers)
          } else {
            self.selectedUsers = val.map((id) => self.allUsersMap[id]).filter((row) => row && row.id)
            console.log("🤡 ~  self.selectedUsers:", self.allUsersMap, self.selectedUsers)

            // val.forEach((id) => {
            //   if (self.selectedUsersIds.indexOf(id) == -1) {
            //     self.selectedUsersIds.push(id)
            //   }
            // })
          }
            // self.$forceUpdate()
          })
        }
      },
      {
        label: '已选人员',
        prop: 'userIdsTags'
        // formslot: true,
      }
    ]
  })
}
export const treeFormOption = function () {
  const self = this
  return {
    span: 24,
    labelWidth: 120,
    submitBtn: false,
    emptyBtn: false,
    column: [
      {
        label: '角色名称',
        prop: 'roleName',
        hide: true,
        rules: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        search: true,
        disabled: true,
        overHidden: true
      },
      {
        label: '菜单权益',
        prop: 'permissionIds',
        type: 'tree',
        filter: false,
        multiple: true,
        // rules: [
        //   { required: true, message: "菜单权益不能为空", trigger: "change" },
        // ],
        props: {
          label: 'name',
          value: 'id',
          id: 'id'
        },
        dicData: self.menuList,
        tags: true
      }
    ]
  }
}
